package com.ohs.bll.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ohs.bll.service.BookingService;
import com.ohs.bll.util.DateUtil;
import com.ohs.dal.dao.BookingDAO;
import com.ohs.dal.model.Booking;

public class BookingServiceImpl implements BookingService {

	private BookingDAO bookingDAO;

	public void saveOrUpdate(Booking booking) {
		bookingDAO.saveOrUpdate(booking);
	}

	public void udpate(Booking Booking) {
		bookingDAO.update(Booking);
	}

	public Booking get(long BookingId) {
		return bookingDAO.get(BookingId);
	}

	public List<Booking> list() {
		return bookingDAO.list();
	}

	public List<Booking> findByRoom(long roomId) {
		return bookingDAO.findByRoom(roomId);
	}

	public BookingDAO getBookingDAO() {
		return bookingDAO;
	}

	public void setBookingDAO(BookingDAO bookingDAO) {
		this.bookingDAO = bookingDAO;
	}

	public boolean delete(Long bookingId) {
		Booking booking = bookingDAO.get(bookingId);
		if (booking != null) {
			this.bookingDAO.delete(booking);
			return true;
		}
		return false;
	}

	public int countTodayBooking(long roomId, Date date) {
		try {
			String theDay = DateUtil.getDate(date);
			Date fromDate = DateUtil.stringToDate(theDay, "00:00");
			Date toDate = DateUtil.stringToDate(theDay, "23:59");
			return bookingDAO.countBookingWithin(roomId, fromDate, toDate);
		} catch (Exception e) {
			return 0;
		}
	}

	public List<Booking> findByRoomAndDate(long roomId, Date date) {
		try {
			String theDay = DateUtil.getDate(date);
			Date fromDate = DateUtil.stringToDate(theDay, "00:00");
			Date toDate = DateUtil.stringToDate(theDay, "23:59");
			return bookingDAO.getBookingWithin(roomId, fromDate, toDate);
		} catch (Exception e) {
			return new ArrayList<Booking>();
		}
	}

	public List<Booking> findByDate(Date bookingDate) {
		return bookingDAO.findByDate(bookingDate);
	}
}
